<main class="" th:fragment="userAdmin">
    <body>


    <table id="dg" title="用户管理" class="easyui-datagrid" style="width:1280px;height:500px"
           url="/user/s"
           toolbar="#toolbar" pagination="true"
           rownumbers="true" fitColumns="true" singleSelect="true">
        <thead>
        <tr>
            <th field="id" width="50">用户ID</th>
            <th field="account" width="50">用户账号</th>
            <th field="name" width="50">用户昵称</th>
            <th field="pw" width="50">用户密码</th>
            <th field="sex" width="50">用户性别</th>
            <th field="iphone" width="50">用户手机</th>
        </tr>
        </thead>
    </table>
    <div id="toolbar">

        <a th:if="${userMessage.lv == '0'}" href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">添加用户</a>
        <a th:if="${userMessage.lv == '0'}" href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">修改用户</a>
        <a th:if="${userMessage.lv == '0'}" href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">删除用户</a>
        <div id="tb" style="padding:3px">
            <span>用户账号:</span>
            <input id="account" style="line-height:26px;border:1px solid #ccc">
            <span>用户昵称:</span>
            <input id="name" style="line-height:26px;border:1px solid #ccc">
            <span>用户手机:</span>
            <input id="iphone" style="line-height:26px;border:1px solid #ccc">
            <a href="#" class="easyui-linkbutton" plain="true" onclick="doSearch()">搜索</a>
        </div>
    </div>

    <div id="dlg" class="easyui-dialog" style="width:400px"
         data-options="closed:true,modal:true,border:'thin',buttons:'#dlg-buttons'">
        <form  id="fm" method="post" novalidate style="margin:0;padding:20px 50px">
            <h3>用户信息</h3>
            <div style="margin-bottom:10px">
                <input class="easyui-combobox" name="lv" label="用户级别:" style="width:100%"
                       data-options="valueField:'lv',textField:'name',url:'/user/select'">
            </div>
            <div style="margin-bottom:10px">
                <input name="account" class="easyui-textbox" required="true" validType="email" label="用户账号:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="name" class="easyui-textbox" required="true" label="用户昵称:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="pw" class="easyui-textbox" required="true" label="用户密码:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="sex" class="easyui-textbox" required="true" label="用户性别:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input name="iphone" class="easyui-textbox" required="true"  label="手机号:" style="width:100%">
            </div>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveUser()"
           style="width:90px">Save</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel"
           onclick="javascript:$('#dlg').dialog('close')" style="width:90px">Cancel</a>
    </div>
    <script type="text/javascript">
        var url;
        //搜索
        function doSearch(){
            var data = {};
            if( $('#account').val()){
                data.account = $('#account').val();
            }
            if( $('#name').val()){
                data.name = $('#name').val();
            }
            if( $('#iphone').val()){
                data.iphone = $('#iphone').val();
            }
            $('#dg').datagrid('load',data);
        }

        //添加
        function newUser() {
            $('#dlg').dialog('open').dialog('center').dialog('setTitle', '新增用户');
            $('#fm').form('clear');
            url = '/user/add';
        }
        //修改
        function editUser() {
            var row = $('#dg').datagrid('getSelected');
            if (row) {
                $('#dlg').dialog('open').dialog('center').dialog('setTitle', '修改用户');
                $('#fm').form('load', row);
                url = '/user/up?id=' + row.id;
            }
        }
        //添加和修改保存用户
        function saveUser() {
            $('#fm').form('submit', {
                url: url,
                onSubmit: function () {
                    return $(this).form('validate');
                },
                success: function (result) {
                    if (result == 'false-has-double') {
                        $.messager.show({
                            title: 'Error',
                            msg: '重名'
                        });
                    } else {
                        var result = eval('(' + result + ')');
                        $('#dlg').dialog('close');        // close the dialog
                        $('#dg').datagrid('reload');    // reload the warehouse data
                    }
                }
            });
        }
        //删除
        function destroyUser() {
            var row = $('#dg').datagrid('getSelected');
            if (row) {
                $.messager.confirm('Confirm', '确定要销毁此用户吗?', function (r) {
                    if (r) {
                        $.post('/user/del', {id: row.id}, function (result) {
                            if (result) {
                                $('#dg').datagrid('reload');    // reload the user data
                            } else {
                                $.messager.show({    // show error message
                                    title: 'Error',
                                    msg: '不允许删除'
                                });
                            }
                        }, 'json');
                    }
                });
            }
        }
    </script>
    </body>
</main>